﻿using System;
using System.Linq;

namespace WebService_Newstore.DAO
{
    public class UserDAO : BaseDAO
    {
        public int idUserDAO { get; set; }
        public string nomUserDAO { get; set; }
        public string preUserDAO { get; set; }
        public string loginUserDAO { get; set; }
        public string mdpUserDAO { get; set; }

        public UserDAO(int idUser)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var userQuery = from utilisateur in newStoreContext.utilisateurs
                                    where utilisateur.id_user == idUser
                                    select utilisateur;

                    if (userQuery.LongCount() == 0)
                    {
                        //On instancie un userDAO vide pour faire remonter l'erreur au niveau du controller
                        this.idUserDAO = 0;
                        this.preUserDAO = string.Empty;
                        this.nomUserDAO = string.Empty;
                        this.loginUserDAO = string.Empty;
                        this.mdpUserDAO = string.Empty;
                    }

                    foreach (var user in userQuery)
                    {
                        //On instancie le UserDAO
                        this.idUserDAO = user.id_user;
                        this.nomUserDAO = user.nom_user;
                        this.preUserDAO = user.pre_user;
                        this.loginUserDAO = user.login;
                        this.mdpUserDAO = user.mdp_user;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }
            }
        }

        public UserDAO(string login, string mdp)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var userQuery = from utilisateur in newStoreContext.utilisateurs
                                    where utilisateur.login == login && utilisateur.mdp_user == mdp
                                    select utilisateur;

                    if (userQuery.LongCount() == 0)
                    {
                        //On instancie un userDAO vide pour faire remonter l'erreur au niveau du controller
                        this.idUserDAO = 0;
                        this.preUserDAO = string.Empty;
                        this.nomUserDAO = string.Empty;
                        this.loginUserDAO = string.Empty;
                        this.mdpUserDAO = string.Empty;
                        throw new Exception("Aucun user ne correspond à ce couple login/mdp !!");
                    }
                    foreach (var user in userQuery)
                    {
                        //On instancie le UserDAO
                        this.idUserDAO = user.id_user;
                        this.nomUserDAO = user.nom_user;
                        this.preUserDAO = user.pre_user;
                        this.loginUserDAO = user.login;
                        this.mdpUserDAO = user.mdp_user;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }
            }
        }
    }
}